/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controle;

import java.util.Collection;
import java.util.List;
import javax.persistence.*;
import model.Curriculo;
import model.Disciplina;
import model.Usuario;
//import view.ApresentacaoGUI;


 /*
 * @author Lucas
 */
public class DisciplinasRestantesService {
    //final private ApresentacaoGUI gui;
    private EntityManager entityManager;
    
    public DisciplinasRestantesService(){
        //this.gui = gui; 
       // entityManager = Persistence.createEntityManagerFactory("ApresentacaoPU").createEntityManager()
    }
    
    public Usuario getUsuarioFromId(int usuario_id){
      entityManager = Persistence.createEntityManagerFactory("ApresentacaoPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Usuario.findById");
        query.setParameter("id", usuario_id);
        List<Usuario> result = query.getResultList();
        if (result.size()!=1) return null;
        //ERROR
        return result.get(0);
        
    }
    
    public void getDisciplinasRestantes(int usuario_id){
          Usuario user = getUsuarioFromId(usuario_id);
         
          
        //OBRIGATORIAS
        Query query = entityManager.createNamedQuery("Curriculo.findByCurriculoid");
        query.setParameter("curriculoid", user.getCurriculoid().getCurriculoid());
        List<Curriculo> resultList = query.getResultList();
        for(Curriculo c : resultList){
            System.out.println(c.getCurso());
        }
        if(resultList.size()!=1){
            System.err.println("Aluno com nenhum ou mais de um curso associado");
        }
        
        Collection<Disciplina> obrigatorias = resultList.get(0).getDisciplinaCollection();
        
        //CURSADAS
        Collection<Disciplina> cursadas = user.getDisciplinaCollection();
        
        //RESULTADO
        
        Collection<Disciplina> restantes = obrigatorias;
        restantes.removeAll(cursadas);
        
        
        for(Disciplina d : restantes){
            //gui.appendDisciplina(d.getCodigo() + " " + d.getNome());
            System.out.println(d.getCodigo() + " " + d.getNome());
        }
    }
    
    public void querySample(){     
//        Query query = entityManager.createNamedQuery("Disciplina.findAll");
//        List<Disciplina> resultList = query.getResultList();
//        for(Disciplina d : resultList){
//            gui.appendDisciplina(d.getCodigo() + " " + d.getNome());
//        }
    }
}